package org.computate.form;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.computate.form.item.FormField;
import org.computate.form.item.FormGroup;
import org.computate.form.item.FormItem;
import org.computate.form.item.FormParagraph;
import org.computate.form.item.PdfChunk;
import org.computate.form.item.PdfChunkField;
import org.computate.form.item.PdfFont;
import org.computate.form.item.PdfHorizontalLine;
import org.computate.form.item.PdfLine;
import org.computate.form.item.PdfLineField;
import org.computate.form.item.PdfNewLine;
import org.computate.model.dao.ComputateDao;
import org.computate.model.dao.Daos;
import org.computate.model.item.Model;
import org.computate.tool.FormTool;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;


public class ComputateForm {
	
	public String getDisplayName() { return "Form"; }
	
	private ArrayList<FormItem> items = new ArrayList<FormItem>();
	private String formName;
	private String formLabel;
	private PdfFont pdfDefaultFont = new PdfFont(false, 10, false, false);
	private PdfFont pdfFont = pdfDefaultFont;
	private Date created = new Date();
	
	public ComputateForm(String formLabel) {
		this.formLabel = formLabel;
		this.formName = this.getClass().getSimpleName();
	}

	
	public ArrayList<FormItem> getItems() {
		return items;
	}

	
	public void setFields(ArrayList<FormItem> items) {
		this.items = items;
	}
	
	protected FormField createField(String name, String label) {
		FormField item = new FormField();
		
		try {
			Field field = this.getClass().getDeclaredField(name);
			Class<?> c = field.getType();
			item.setC(c);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		item.setName(name);
		item.setLabel(label);
		
		return item;
	}
	
	protected FormField addField(String name, String label) {
		FormField item = createField(name, label);
		
		this.items.add(item);
		
		return item;
	}

	public PdfChunk addPdfChunk(String text) {
		PdfChunk item = new PdfChunk(text);
		
		this.items.add(item);
		
		return item;
	}

	public PdfLine addPdfLine(String text) {
		PdfLine item = new PdfLine(text);
		
		this.items.add(item);
		
		return item;
	}

	public PdfNewLine addPdfNewLine() {
		PdfNewLine item = new PdfNewLine();
		
		this.items.add(item);
		
		return item;
	}

	public PdfHorizontalLine addPdfHorizontalLine() {
		PdfHorizontalLine item = new PdfHorizontalLine();
		
		this.items.add(item);
		
		return item;
	}

	public PdfLineField addPdfLineField(String text) {
		PdfLineField item = new PdfLineField(text);
		
		this.items.add(item);
		
		return item;
	}

	public PdfChunkField addPdfChunkField(String text) {
		PdfChunkField item = new PdfChunkField(text);
		
		this.items.add(item);
		
		return item;
	}

	public PdfFont addPdfFont(boolean serif, int size, boolean bold, boolean italic) {
		PdfFont item = new PdfFont(serif, size, bold, italic);
		
		this.items.add(item);
		
		return item;
	}

	public PdfFont addPdfFont(boolean serif, int size) {
		PdfFont item = new PdfFont(serif, size);
		
		this.items.add(item);
		
		return item;
	}
	
	protected FormGroup createGroup(String label) {
		FormGroup group = new FormGroup(this.getClass());
		
		group.setLabel(label);
		
		return group;
	}
	
	protected FormGroup addGroup(String label) {
		FormGroup group = createGroup(label);
		
		this.items.add(group);
		
		return group;
	}
	
	protected FormParagraph createParagraph(String text) {
		FormParagraph item = new FormParagraph();
		
		item.setText(text);
		
		return item;
	}
	
	protected FormParagraph addParagraph(String text) {
		FormParagraph item = createParagraph(text);
		
		this.items.add(item);
		
		return item;
	}
	
	public void populateFields(Daos daos) {
		for(FormItem itemObject : items) {
			populateField(itemObject, daos);
		}
	}
	
	private void populateField(FormItem itemObject, Daos daos) {
		if(itemObject != null) {
			if(itemObject instanceof FormField) {
				try {
					FormField item = (FormField)itemObject;
				
					if(Model.class.isAssignableFrom(item.getC())) {
						Class<?> c = item.getC();
						String modelName = c.getSimpleName();
						Model newInstance = (Model)c.newInstance();
						
						if(modelName != null) {
							ComputateDao<?> dao = daos.getDao(modelName);
							
							if(dao != null) {
								List<? extends Model> items = dao.list(newInstance.getListLevels());
								
								item.setItems(items);
								item.setItemName(modelName);
								item.setItemDisplayName(newInstance.getModelDisplayName());
								item.setItemPluralName(newInstance.getModelPluralName());
							}
						}
					}
					else if(List.class.isAssignableFrom(item.getC())) {
						Field stringListField = this.getClass().getDeclaredField(item.getName());
				        ParameterizedType genericType = (ParameterizedType)stringListField.getGenericType();
				        Class<?> genericClass = (Class<?>)genericType.getActualTypeArguments()[0];
				        
						if(Model.class.isAssignableFrom(genericClass)) {
							String modelName = genericClass.getSimpleName();
							Model newInstance = (Model)genericClass.newInstance();
							
							if(modelName != null) {
								ComputateDao<?> dao = daos.getDao(modelName);
								
								if(dao != null) {
									List<? extends Model> items = dao.list(newInstance.getListLevels());
									
									item.setItems(items);
									item.setItemName(modelName);
									item.setItemDisplayName(newInstance.getModelDisplayName());
									item.setItemPluralName(newInstance.getModelPluralName());
								}
							}
						}
						else if(ComputateForm.class.isAssignableFrom(genericClass)) {
							String modelName = genericClass.getSimpleName();
							ComputateForm newInstance = (ComputateForm)genericClass.newInstance();
							
							if(modelName != null) {
								newInstance.populateFields(daos);
								item.setForm(newInstance);
							}
						}
					}
					for(FormItem hiddenItem : itemObject.getHiddenItems()) {
						populateField(hiddenItem, daos);
					}
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if(itemObject instanceof FormGroup) {
				FormGroup group = (FormGroup)itemObject;
				
				for(FormItem item : group.getChildren()) {
					populateField(item, daos);
				}
			}
		}
	}
	
	public void populateValues(HttpServletRequest r, Daos daos) {
		if(r != null) {
			for(FormItem itemObject : items) {
				populateValues(r, itemObject, daos);
			}
		}
	}
	
	private void populateValues(HttpServletRequest r, FormItem itemObject, Daos daos) {
		if(r != null && itemObject != null) {
			if(itemObject instanceof FormField) {
				try {
					FormField item = (FormField)itemObject;
					String fieldName = item.getName();
					String valueString = r.getParameter(fieldName);
					
					if(item.getForm() != null) {
						Method setter = new PropertyDescriptor(fieldName, this.getClass()).getWriteMethod();
						int i = 1;
						List<ComputateForm> forms = new ArrayList<ComputateForm>();
						ComputateForm currentForm = (ComputateForm)item.getForm().getClass().newInstance();
						boolean hasValue = false;
						
						for(FormItem formItem : currentForm.items) {
							if(formItem instanceof FormField) {
								FormField formField = (FormField)formItem;
								String currentFieldName = fieldName + "_" + formField.getName();
								Method setter2 = new PropertyDescriptor(formField.getName(), currentForm.getClass()).getWriteMethod();
								String value = r.getParameter(currentFieldName + i);
								
								if(!StringUtils.isEmpty(value)) {
									setter2.invoke(currentForm, value);
									hasValue = true;
								}
							}
						}
						if(hasValue) {
							forms.add(currentForm);
						}
						while(hasValue) {
							i++;
							currentForm = (ComputateForm)item.getForm().getClass().newInstance();
							hasValue = false;
							for(FormItem formItem : currentForm.items) {
								if(formItem instanceof FormField) {
									FormField formField = (FormField)formItem;
									String currentFieldName = fieldName + "_" + formField.getName();
									Method setter2 = new PropertyDescriptor(formField.getName(), currentForm.getClass()).getWriteMethod();
									String value = r.getParameter(currentFieldName + i);
									
									if(!StringUtils.isEmpty(value)) {
										setter2.invoke(currentForm, value);
										hasValue = true;
									}
								}
							}
							if(hasValue) {
								forms.add(currentForm);
							}
						}
						setter.invoke(this, forms);
					}
					if(item.getItems() != null && List.class.isAssignableFrom(item.getC())) {
						Method getter = new PropertyDescriptor(fieldName, this.getClass()).getReadMethod();
						Field stringListField = this.getClass().getDeclaredField(fieldName);
				        ParameterizedType genericType = (ParameterizedType)stringListField.getGenericType();
				        Class<?> genericClass = (Class<?>)genericType.getActualTypeArguments()[0];
						Method setter = new PropertyDescriptor(fieldName, this.getClass()).getWriteMethod();
				        ComputateForm newFormInstance = (ComputateForm)this.getClass().newInstance();
						Model newItemInstance = (Model)genericClass.newInstance();
						Object items = (Object)getter.invoke(newFormInstance, new Object[] {});
						String[] values = r.getParameterValues(item.getName());
						
						if(items != null && values != null) {
							Method addMethod = items.getClass().getDeclaredMethod("add", Object.class);
							
							for(String value : values) {
								Long id = NumberUtils.toLong(value);
								
								if(id > 0) {
									ComputateDao<?> dao = daos.getDao(genericClass.getSimpleName());
									Model modelItem = dao.get(id, newItemInstance.getGetLevels());
									
									if(modelItem != null) {
										addMethod.invoke(items, modelItem);
									}
								}
							}
						}
						setter.invoke(this, items);
					}
					else if(!StringUtils.isEmpty(valueString)) {
						Method setter = new PropertyDescriptor(fieldName, this.getClass()).getWriteMethod();
						if(item.getC().equals(String.class)) {
							setter.invoke(this, valueString);
						}
						else if(item.getC().equals(Boolean.class) || item.getC().equals(boolean.class)) {
							Boolean value = BooleanUtils.toBoolean(valueString);
							
							setter.invoke(this, value);
						}
						else if(item.getC().equals(Integer.class) || item.getC().equals(int.class)) {
							Integer value = NumberUtils.toInt(valueString);
							
							setter.invoke(this, value);
						}
						else if(item.getC().equals(Double.class) || item.getC().equals(double.class)) {
							Double value = NumberUtils.toDouble(valueString);
							
							setter.invoke(this, value);
						}
						else if(item.getC().equals(Long.class) || item.getC().equals(long.class)) {
							Long value = NumberUtils.toLong(valueString);
							
							setter.invoke(this, value);
						}
						else if(item.getC().equals(Date.class)) {
							try {
								Date value = DateUtils.parseDate(valueString, "MM/dd/yyyy");
								
								setter.invoke(this, value);
							} catch (ParseException e) {
								// ignore if date is invalid
							} catch (InvocationTargetException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						else if(Model.class.isAssignableFrom(item.getC())) {
							try {
								ComputateDao<?> dao = daos.getDao(item.getC().getSimpleName());
								Long id = NumberUtils.toLong(valueString);

								if(id > 0) {
									Model newInstance = (Model)item.getC().newInstance();
									Model modelItem = dao.get(id, newInstance.getGetLevels());
									
									setter.invoke(this, modelItem);
								}
							} catch (InstantiationException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						for(FormItem hiddenItem : itemObject.getHiddenItems()) {
							populateValues(r, hiddenItem, daos);
						}
					}
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IntrospectionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if(itemObject instanceof FormGroup) {
				FormGroup group = (FormGroup)itemObject;
				
				for(FormItem item : group.getChildren()) {
					populateValues(r, item, daos);
				}
			}
		}
	}
	
	public void sendInEmail(HttpServletRequest request, HttpServletResponse response, Daos daos) throws MessagingException, DocumentException, IOException {
		Properties props = new Properties();
		Session session = Session.getInstance(props);
		StringBuilder msgBody = new StringBuilder();
        MimeMessage msg = new MimeMessage(session);
		Multipart mp = new MimeMultipart();
		MimeBodyPart htmlPart = new MimeBodyPart();
        MimeBodyPart attachment = new MimeBodyPart();
        DataSource dataSource = getPdfDataSource(request, daos);
        
        if(dataSource != null) {
            String name = getFormName();
            
            msgBody.append("Attached is a " + getDisplayName() + ".");
            msg.setFrom(new InternetAddress("Computating@toshmd.com", name));
			if(request.getServerName().equals("toshmd-dev.appspot.com"))
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress("computating@gmail.com", "The Orthopedic Specialty Clinic at TOSH"));
			else {
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress("tosc@imail.org", "The Orthopedic Specialty Clinic at TOSH"));
			}
            msg.setSubject(getDisplayName());
			htmlPart.setContent(msgBody.toString(), "text/html");
			mp.addBodyPart(htmlPart);
            
            attachment.setFileName(name.replaceAll("\\s", "_") + ".pdf");
            attachment.setDataHandler(new DataHandler(dataSource));
            mp.addBodyPart(attachment);
            
            msg.setContent(mp);
            Transport.send(msg);
        }
	}
	
	private ByteArrayDataSource getPdfDataSource(HttpServletRequest request, Daos daos) throws DocumentException, IOException {
		ByteArrayDataSource result = null;
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		
		try {
			Document d = new Document();
			byte[] bytes = null;
			PdfWriter.getInstance(d, outputStream);
			
			generatePdf(request, d, daos);
			d.close();
			bytes = outputStream.toByteArray();
			
			result = new ByteArrayDataSource(bytes, "application/pdf");
		}
		finally {
			outputStream.close();
		}
		
		return result;
	}
	
	public void downloadPdf(HttpServletRequest request, HttpServletResponse response, Daos daos) throws DocumentException, IOException {
		if(response != null) {
			Document d = new Document();
			OutputStream outputStream = response.getOutputStream();
			String fileName = formLabel.replaceAll("\\s", "_");
			PdfWriter.getInstance(d, outputStream);
			generatePdf(request, d, daos);
			
			response.setContentType("application/pdf");
			response.addHeader("content-disposition", "attachment; filename=" + fileName);
			
			d.close();
			outputStream.flush();
			outputStream.close();
		}
	}
	
	public Document generatePdf(HttpServletRequest r, Document d, Daos daos) throws DocumentException {
		if (d != null) {
			Paragraph mainTitle = new Paragraph();
			
			d.open();
			d.addTitle(formLabel);
			d.addAuthor("toshmd.com");
			d.addTitle("toshmd.com");
			try {
				Image image = Image.getInstance(new URL("http://www.toshmd.com/image/logo.png"));
				
				image.scaleToFit(130, 130);
				mainTitle.add(new Chunk(image, 0, 0, true));
			} catch (MalformedURLException e) {
				// ignore
			} catch (IOException e) {
				// ignore
			}
			mainTitle.setFont(new Font(FontFamily.HELVETICA, 14));
			mainTitle.add(new Chunk(formLabel));
			d.add(mainTitle);
			
			for(FormItem itemObject : items) {
				generatePdf(itemObject, d, daos);
			}
		}
		
		return d;
	}
	
	private void generatePdf(FormItem itemObject, Document d, Daos daos) throws DocumentException {
		PdfPTable t = new PdfPTable(2);
		
		generatePdf(itemObject, d, t, daos);
		t.completeRow();
		d.add(t);
	}
	
	private void generatePdf(FormItem itemObject, Document d, PdfPTable t, Daos daos) throws DocumentException {
		if(itemObject instanceof FormParagraph) {
			FormParagraph item = (FormParagraph)itemObject;
			
			addLine(d, item.getText());
		}
		else if(itemObject instanceof FormField) {
			FormField item = (FormField)itemObject;
			String value = item.getValueString(this);
			List<FormItem> hiddenItems = item.getHiddenItems();
			
			if(List.class.isAssignableFrom(item.getC())) {
				try {
					Field stringListField = this.getClass().getDeclaredField(item.getName());
					ParameterizedType genericType = (ParameterizedType)stringListField.getGenericType();
					Class<?> genericClass = (Class<?>)genericType.getActualTypeArguments()[0];
					
					if(Model.class.isAssignableFrom(genericClass)) {
						List<?> items = (List<?>)item.getValue(this);
						StringBuilder result = new StringBuilder();
						int index = 0;
						if(items != null) {
							for(Object o : items) {
								if(o instanceof Model) {
									Model i = (Model)o;
									
									if(index > 0) {
										result.append(", ");
									}
									result.append(i.getName());
								}
								index++;
							}
						}
						addInput(t, item.getLabel(), result.toString());
					}
					else if(ComputateForm.class.isAssignableFrom(genericClass)) {
						ComputateForm newInstance = (ComputateForm)genericClass.newInstance();
						List<?> forms = (List<?>)item.getValue(this);
						
						newInstance.populateFields(daos);
						
						int columns = newInstance.getItems().size();
						
						t = new PdfPTable(columns);
						t.setWidthPercentage(100);
						for(FormItem formItem : newInstance.getItems()) {
							addHeader(t, formItem.getLabel());
						}
						for(Object formObject : forms) {
							if(formObject instanceof ComputateForm) {
								ComputateForm form = (ComputateForm)formObject;
								
								for(FormItem formItem : form.getItems()) {
									FormField item2 = (FormField)formItem;
									String value2 = item2.getValueString(form);
									
									addText(t, value2);
								}
							}
						}
						t.completeRow();
						d.add(t);
					}
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else {
				addInput(t, item.getLabel(), value);
			}
			if(hiddenItems.size() > 0 && "yes".equals(value)) {
				for(FormItem itemObject2 : hiddenItems) {
					generatePdf(itemObject2, d, t, daos);
				}
			}
		}
		else if(itemObject instanceof FormGroup) {
			FormGroup formGroup = (FormGroup)itemObject;
			int columns;
			
			if(formGroup.getColumns() == null)
				columns = 1;
			else
				columns = formGroup.getColumns();
			float[] widths = new float[columns * 2];
			float width1 = 100F / (float)columns * .1F;
			float width2 = 100F / (float)columns * .9F;
			
			d.add(new Chunk(formGroup.getLabel(), new PdfFont(false, 14, true, false).getFont()));
			d.add(Chunk.NEWLINE);
			t = new PdfPTable(columns * 2);
			for(int i = 0; i < columns; i++) {
				widths[i * 2] = width1;
				widths[i * 2 + 1] = width2;
			}
			t.setWidthPercentage(100);
			for(FormItem formItem : formGroup.getChildren()) {
				generatePdf(formItem, d, t, daos);
			}
			t.completeRow();
			d.add(t);
		}
		else if(itemObject instanceof PdfChunk) {
			PdfChunk item = (PdfChunk)itemObject;
			
			d.add(new Chunk(item.getText(), pdfFont.getFont()));
		}
		else if(itemObject instanceof PdfLine) {
			PdfLine item = (PdfLine)itemObject;
			
			d.add(new Chunk(item.getText(), pdfFont.getFont()));
			d.add(Chunk.NEWLINE);
		}
		else if(itemObject instanceof PdfNewLine) {
			d.add(Chunk.NEWLINE);
		}
		else if(itemObject instanceof PdfHorizontalLine) {
			d.add(new LineSeparator(1, 100, null, Element.ALIGN_BASELINE, -3));
		}
		else if(itemObject instanceof PdfLineField) {
			PdfLineField item = (PdfLineField)itemObject;
			String value = FormTool.getValueString(this, item.getName());
			
			if(value == null)
				value = "";
			d.add(new Chunk(value, pdfFont.getFont()));
			d.add(Chunk.NEWLINE);
		}
		else if(itemObject instanceof PdfChunkField) {
			PdfChunkField item = (PdfChunkField)itemObject;
			String value = FormTool.getValueString(this, item.getName());
			
			if(value == null)
				value = "";
			d.add(new Chunk(value, pdfFont.getFont()));
		}
		else if(itemObject instanceof PdfFont) {
			PdfFont item = (PdfFont)itemObject;
			
			pdfFont = item;
		}
	}
	
	protected void addLine(Document d, String text) throws DocumentException {
		d.add(new Chunk(text, pdfFont.getFont()));
		d.add(Chunk.NEWLINE);
	}
	
	protected void addInput(PdfPTable t, String name, String value, Integer colspan) {
		if(t != null) {
			Phrase p1 = new Phrase(name, pdfFont.getFont());
			Phrase p2 = new Phrase(value, pdfFont.getFont());
			PdfPCell cell1 = new PdfPCell(p1);
			PdfPCell cell2 = new PdfPCell(p2);
			
			cell1.setBorder(0);
			cell2.setBorder(0);
			cell1.setPadding(2.2F);
			cell2.setPadding(2.2F);
			if(colspan != null)
				cell2.setColspan(colspan);
			cell1.setHorizontalAlignment(Element.ALIGN_RIGHT);
			cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
			if(!StringUtils.isEmpty(name))
				p2.add(new LineSeparator(1, 100, null, Element.ALIGN_BASELINE, -3));
			t.addCell(cell1);
			t.addCell(cell2);
		}
	}
	
	protected void addInput(PdfPTable t, String name, String value) {
		addInput(t, name, value, null);
	}
	
	protected void addText(PdfPTable t, String value) {
		if(t != null) {
			Phrase p1 = new Phrase(value, pdfFont.getFont());
			PdfPCell cell1 = new PdfPCell(p1);
			
			cell1.setBorder(0);
			cell1.setPadding(2.2F);
			cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
			t.addCell(cell1);
		}
	}
	
	protected void addHeader(PdfPTable t, String value) {
		if(t != null) {
			Phrase p1 = new Phrase(value, pdfFont.getFont());
			PdfPCell cell1 = new PdfPCell(p1);
			
			cell1.setBorder(0);
			cell1.setPadding(0F);
			cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
			p1.add(new LineSeparator(1, 100, null, Element.ALIGN_BASELINE, -3));
			t.addCell(cell1);
		}
	}
	
	protected void addInput(PdfPTable t, String name, Integer value) {
		String newValue = null;
		
		if(value != null)
			newValue = value.toString();
		addInput(t, name, newValue, null);
	}

	public String getFormName() {
		return formName;
	}


	public void setFormName(String formName) {
		this.formName = formName;
	}


	public String getFormLabel() {
		return formLabel;
	}


	public void setFormLabel(String formLabel) {
		this.formLabel = formLabel;
	}
	
	public void submit(Daos daos) {
		
	}
	public Date getCreated() {
		return created;
	}
	public void setCreated(Date created) {
		this.created = created;
	}
}
